﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using TouchUI.System.Mobile.Mvc;
using System.Mobile.Mvc;

using LinkedService.Mobile.ServiceEntities;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using LinkedIn.Mobile.Properties;

namespace LinkedIn.Mobile
{
  public class ConnectionsDialog : ViewDialog, IView<ConnectionsModel>
  {

    [PublishEvent("OnBack")]
    public event EventHandler OnBackEvent;

    [PublishEvent("OnProfileSelected")]
    public event PersonEventHandler OnProfileSelected; 
   
    private ScrollList scrollList;
    private AlphabetBar alphabetBar;

    public ConnectionsDialog()
    {
      ScreenFactor  = Common.Instance.ScreenFactor;
      Rectangle     =  Common.Instance.ClientRectangle;
      scrollList = new ScrollList()
      {
        Rectangle       = new Rectangle(0, 0, Rectangle.Width, Rectangle.Height),
        ListItemHeight  = ConnectionListItem.ItemHeight,
        ScreenFactor    = ScreenFactor,
        HighBrush       = Common.Instance.HighBrush,
        GrayBrush       = Common.Instance.GrayBrush
      };

      alphabetBar = new AlphabetBar()
      {
        Rectangle     = new Rectangle(0, 0, Rectangle.Width, Rectangle.Height),
        ScreenFactor  = ScreenFactor,
        Image         = Resources.bar
      };

      HasScrollControl = true;
      scrollList.ScrollHeight = 0;
      scrollList.Selected += new EventHandler(scrollList_Selected);
      alphabetBar.Selected += new EventHandler<SelectedEventArgs>(alphabetBar_Selected);
    }

    void alphabetBar_Selected(object sender, SelectedEventArgs e)
    {
      scrollList.MoveToItem(e.Char.ToString());
     // MessageBox.Show(e.Char.ToString());
    }

    void scrollList_Selected(object sender, EventArgs e)
    {
      ConnectionListItem item = scrollList.ListItems[scrollList.SelectedLine] as ConnectionListItem;
      if (item != null)
      {
        if (OnProfileSelected != null)
          OnProfileSelected(this, new PersonEventArgs() { Person = item.ContainedItem as Person, Id = item.Id, Distance = item.Distance });
      }
    }

    private string lastheader = " ";

    /// <summary>
    /// Add a header in case we moved to the next char in the alphabet
    /// </summary>
    /// <param name="p"></param>
    private void AddHeader(Person p)
    {
      string group = p.LastName[0].ToString().ToUpper();
      if (group.CompareTo(lastheader) != 0)
        scrollList.ListItems.Add(new HeaderListItem(group.ToUpper()));
      lastheader = group;

    }

     // Callback from the Controller
    private void OnConnectionsLoaded(object sender, EventArgs e)
    {
      Collection<Person> _data = null;
      scrollList.ListItems.Clear();
      // Check to see which data we should show.
      if (Model.ShowConnections)
        _data = Model.Connections.Items;
      else
        _data = Model.People.Items;

      // If the number of persons exceeds a certain number, use header columns.
      bool useHeader = _data.Count > ApplicationManager.Configuration.GetItem<int>("", 10);
      for (int i = 0; i < _data.Count; i++)
      {
        // Only use a header in case we have more than a specific amount of users.
        if (useHeader) AddHeader(_data[i]);
        // Update the distance in case we are dealing with own connections.
        // We do not get the correct distance from LinkedIn.
        _data[i].Distance = Model.ShowConnections?1:_data[i].Distance;
        scrollList.ListItems.Add(new ConnectionListItem(i, _data[i]));
      }
      // If we have headers, show the alphabetbar.
      alphabetBar.BarIsVisible = useHeader;

      scrollList.ScrollHeight = scrollList.ListItemHeight * scrollList.ListItems.Count;
      Cursor.Current = Cursors.Default;
    }

    public override void Paint(Graphics g)
    {
      g.Clear(BackColor);
      // Let the scrolllist do its painting
      scrollList.Paint(g);
      alphabetBar.Paint(g);
    }
 
    public override void MouseDown(Point p)
    {
      scrollList.MouseDown(p);
    }

    public override void MouseMove(Point p)
    {
      scrollList.MouseMove(p);
    }

    public override void MouseUp(Point p)
    {
      if (alphabetBar.Bounds.Inside(p))
        alphabetBar.MouseUp(p);
      else
        scrollList.MouseUp(p);
    }

    public override void KeyDown(KeyEventArgs e)
    {
      scrollList.KeyDown(e);
    }

    public override void KeyUp(KeyEventArgs e)
    {
      scrollList.KeyUp(e);
    }

    public override void Resize(Rectangle r)
    {
      this.Rectangle = r;
      scrollList.Resize(r);
      //scrollList.Rectangle = new Rectangle(0, 0, r.Width, r.Height - 50);
      alphabetBar.Resize(r);
    }


    #region IView<ConnectionsModel> Members

    public new ViewDataDictionary<ConnectionsModel> ViewData
    {
      get;
      set;
    }

    public new ConnectionsModel Model
    {
      get
      {
        return this.ViewData.Model;
      }
      set
      {
        this.ViewData.Model = value;
      }
    }

    #endregion
  }
}
